<?php
	
function list_pendidikan(){
//PASANG JAVASCRIPT
my_set_code_js('

function confirmDelete(id){
	var t = confirm(\'Yakin akan menghapus data ?\');
	if(t){
		location.href=\'index.php?com=pendidikan&task=delete&id=\'+id;
	}
	return false;
}

');

	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:15px'),  
		'Jenjang'=>array('style'=>'border-bottom:2px solid;width:135px'),   
		'Pendidikan'=>array('style'=>'border-bottom:2px solid;width:250px'),  
		'Kejuruan'=>array('style'=>'border-bottom:2px solid;width:100px'),  
		' '=>array('style'=>'border-bottom:2px solid;width:50px'), 
	);
	$com = isset($_GET['com']) ? $_GET['com'] : '';
	$task = isset($_GET['task']) ? $_GET['task'] : '';
	$field = isset($_GET['field']) ? $_GET['field'] : '';
	$key = isset($_GET['key']) ? $_GET['key'] : '';
	$query = "SELECT *,b.label as level ,a.label as sekolah FROM pendidikan a 
		INNER JOIN level_pendidikan b ON a.level_pendidikan_id = b.level_pendidikan_id 
		ORDER BY a.level_pendidikan_id DESC";
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =7;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		  
	$pager_url  ="index.php?com={$com}&task={$task}&field={$field}&key={$key}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	$pager_url_last = null;
	
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,
				$pager_url_last); 
				
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com=pendidikan&task=editpendidikan&id='.$ey['pendidikan_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['pendidikan_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'lsv'=> '( '.$ey['level_pendidikan_id'] .' ) '. $ey['level'], 
			'l'=>  $ey['sekolah'] .' ('. $ey['code'] .')' ,    
			'm'=>  $ey['major']  ,    
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row);  
	$menuontop = array(  
		'Tambah data'=>array('onclick'=>'javascript:jQuery.facebox({ ajax: \'index.php?com=pendidikan&task=editpendidikan&id=0\' })'),
		//'Export to excel'=>array('onclick'=>'javascript:location.href=\'index.php?com=csv&task=excel_daftar_karyawan\''),
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com=report&task=all_karyawan\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');')
	); 
	$printmenu = additional_menu_on_list( $menuontop ,'');
	$paging = $kgPagerOBJ ->showPaging();
	return  table_builder($header , $datas ,  5 , false, $paging  );
}

function form_pendidikan_validate($id){
	$errsubmit = false;
	$err = array();
	 
	$code =  trim($_POST["code"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] ="Kode pendidikan belum di isi";
	}
	
	$query="SELECT pendidikan_id FROM pendidikan 
		WHERE code='".$code.
		"' AND pendidikan_id<>".$id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Kode pendidikan sudah digunakan";	
	}
	$code =  trim($_POST["level_pendidikan_id"]); 
	if( $code == '0'){
		$errsubmit = true;
		$err[] ="Level pendidikan belum di pilih";
	}
	
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function form_pendidikan_submit($id){
	$datas = array(
		'level_pendidikan_id'=>my_type_data_str( $_POST['level_pendidikan_id'] ),
		'code'=>my_type_data_str( $_POST['code'] ),
		'label'=>my_type_data_str( $_POST['label'] ), 
		'major'=>my_type_data_str( $_POST['major'] ), 
	);
	if($id > 0){
		return my_update_record('pendidikan' ,'pendidikan_id' , $id , $datas);
	}
	
	return my_insert_record('pendidikan' , $datas);
}

function form_pendidikan_create($id = 0){
	$view = form_header( "pendidikan" , "pendidikan"  );
 	$fields = my_get_data_by_id('pendidikan','pendidikan_id',$id);
	$code = array(
				'name'=>'code',
				'value'=>( isset($_POST['code']) ? $_POST['code'] : $fields['code'] ),
				'id'=>'code',
				'type'=>'text',
				'size'=>'15'
			);
	$form_field_code = form_dynamic($code);
	
	$view .= form_field_display( $form_field_code   , "Kode"   );
	
	$datas = array();
	$result = my_query("SELECT * FROM level_pendidikan ORDER BY level_pendidikan_id ASC");
	while( $row = my_fetch_array($result) ){
		$datas[$row['level_pendidikan_id']] = $row['label'];
	}
	$level_pendidikan  = array(
				'name'=>'level_pendidikan_id',
				'value'=>( isset($_POST['level_pendidikan_id']) ? $_POST['level_pendidikan_id'] : $fields['level_pendidikan_id'] ),
				'id'=>'level_pendidikan_id', 
			);
	$form_field_level_pendidikan = form_dropdown($level_pendidikan , $datas );
	$view .= form_field_display( $form_field_level_pendidikan   ,  "Jenjang pendidikan"  );
	
	$label = array(
				'name'=>'label',
				'value'=>( isset($_POST['label']) ? $_POST['label'] : $fields['label'] ),
				'id'=>'label',
				'type'=>'label',
				'size'=>'45'
			);
	$form_field_major = form_dynamic($label);
	
	$view .= form_field_display( $form_field_major  , "Nama pendidikan"  );   
	
	$major = array(
				'name'=>'major',
				'value'=>( isset($_POST['major']) ? $_POST['major'] : $fields['major'] ),
				'id'=>'major',
				'type'=>'major',
				'size'=>'45'
			);
	$form_field_mmajor = form_dynamic($major);
	
	$view .= form_field_display( $form_field_mmajor  , "Kejuruan"  );
	
		$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 

function remove_pendidikan($id){
	$query= "DELETE FROM pendidikan WHERE pendidikan_id = {$id}";
	return my_query($query);
} 